package com.chen.controller;

import com.chen.entity.Result;
import com.chen.service.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/auth")
public class AuthController {
    @Autowired
    private AuthService authService;

    /**
     * 登录接口
     *
     * @param username
     * @param password
     * @return
     */
    @PostMapping("/login")
    public ResponseEntity<Result<Object>> login(@RequestParam("username") String username, @RequestParam("password") String password) {


        //验证token (写在拦截器里面了)   //正确,直接生成新的token返回

        //错误或者没有, 验证码账号密码,生成新的token返回
        String token = authService.login(username, password);
        //把token放在header里面
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("Authorization", "Bearer " + token); //这里存储的数据在header里面
        httpHeaders.add("Access-Control-Expose-Headers", "Authorization");
        Result<Object> result = Result.success("这里的数据返回到body里面");
        ResponseEntity<Result<Object>> responseEntity = new ResponseEntity<>(result, httpHeaders, HttpStatus.OK);
        return responseEntity;

    }


}
